package com.distribute.application.api.rest.delivery;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.application.api.utils.Arith;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.Account;
import com.distribute.framework.model.Distributor;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.Region;
import com.distribute.framework.model.Replenish;
import com.distribute.framework.model.Replenishment;
@RestController(value="api.DeliveryUserInfoController")
public class DeliveryUserInfoController extends BaseApiController{
	/**
	 * 配送商获取基本信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/delivery/deliveryGetInfos.do")
	public @title("配送商获取基本信息") Protocol deliveryGetInfos(
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		if(distributor==null){
			protocol.set("type", 1);
			protocol.set("msg", "您的信息不存在");
		}else {
			protocol.set("type", 2);
			if (distributor.getLocation()!=null) {
				Region country=Region.where.id(distributor.getLocation()).get();
				Region area=Region.where.id(country.getParent()).get();
				Region city=Region.where.id(area.getParent()).get();
				Region province=Region.where.id(city.getParent()).get();
				protocol.set("province",province.toJSON() );
				protocol.set("city",city.toJSON());
				protocol.set("area", area.toJSON());
				protocol.set("country",country.toJSON());
			}else {
				protocol.set("province",null);
				protocol.set("city",null);
				protocol.set("area", null);
				protocol.set("country",null);
			}
			protocol.set("delivery", distributor.toJSON());
		}
		protocol.set("type", 2);
    	return protocol;
	}
	/**
	 * 配送商获取账户信息
	 */
	@RequestMapping("/api/delivery/getAccountsData.do")
	public @title("配送商获取账户信息") Protocol getAccountsData(
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.member(member.getId()).get();
		Distributor distributor=Distributor.where.member(member.getId()).get();
		protocol.set("obj", distributor.toJSON());
		Map map=account.toJSON();
		map.put("money", Arith.round(account.getMoney(), 2));
		protocol.set("acc", map);
    	return protocol;
	}
	/**
	 * 配送商获取补货列表
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/delivery/getBHLists.do")
	public @title("配送商获取补货列表") Protocol getBHLists(
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		
		protocol.setPage(Replenish.where.distributor(distributor.getId()).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<Replenish>(){
			@Override
			public Map format(Replenish t) {
				Map json = t.toJSON();
				List<Replenishment> replenishments=Replenishment.where.replenish(t.getId()).list(-1);
				List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
				if (replenishments.size()>0) {
					for (Replenishment replenishment : replenishments) {
						Map<String,Object> jsons =replenishment.toJSON();
						list.add(jsons);
					}
				}
				json.put("childs", list);
				return json;
			}
		});
		
		return protocol;
	}
}
